package com.park.platform.service.impl;

import com.park.common.bean.AreaView;
import com.park.common.poo.AreaInfo;
import com.park.common.util.StrUtil;
import com.park.platform.dao.IAreaDao;
import com.park.platform.dao.IBaseDao;
import com.park.platform.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AreaServiceImpl implements IAreaService {

    @Autowired
    private IBaseDao baseDao;

    @Autowired
    private IAreaDao areaDao;

    @Override
    public List<AreaView> getAreaListByParentId(String areaParentId) {

        List<AreaInfo> areaList = baseDao.queryByHql("from AreaInfo a where a.areaParentId = ?", areaParentId);
        List<AreaView> areaViewList = new ArrayList<AreaView>();

        for(AreaInfo area : areaList){
            AreaView areaView = new AreaView();
            areaView.setAreaId(area.getAreaId());
            areaView.setAreaParentId(area.getAreaParentId());
            areaView.setAreaName(area.getAreaName());
            areaViewList.add(areaView);
        }
        return areaViewList;
    }

    @Override
    public List<String> getDefaultValueListByAreaId(String areaId){
        return areaDao.getDefaultValueListByAreaId(areaId);
    }

    @Override
    public AreaInfo getById(String areaId){
        return baseDao.getToEvict(AreaInfo.class, areaId);
    }

    @Override
    public String getAreaFullNames(String areaId){
        if(StrUtil.isBlank(areaId)) return StrUtil.EMPTY;
        StringBuilder names = new StringBuilder();
        AreaInfo area = getById(areaId);
        if(area == null) return StrUtil.EMPTY;
        names.append(area.getAreaName());
        int i = 1, j = Integer.valueOf(area.getRank());
        while (i < j){
            area = getById(area.getAreaParentId());
            names.insert(0, area.getAreaName());
            i++;
        }
        return names.toString();
    }

}
